package org.yufan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.yufan.service.BaseService;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by zxd on 2018/3/24.
 */
public  class BaseServiceImpl<T> implements BaseService<T>{

    @Autowired
    private Mapper<T> mapper;


    @Override
    public T queryById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public T queryByWhere(T t) {
        return mapper.selectOne(t);
    }

    @Override
    public List<T> queryListByWhere(T t) {
        return mapper.select(t);
    }

    @Override
    public PageInfo<T> queryPageListByWhere(Integer page,Integer rows,T t) {
        PageHelper.offsetPage(page,rows);
        List<T> list = mapper.select(t);
        PageInfo<T> pageInfo=new PageInfo<T>(list);
        return pageInfo;
    }

    @Override
    public void save(T t) {
        mapper.insertSelective(t);
    }

    @Override
    public void update(T t) {
       mapper.updateByPrimaryKeySelective(t);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByWhere(Class clazz, String property, List<Object> values) {
        Example example=new Example(clazz);
        example.createCriteria().andIn(property,values);
        mapper.deleteByExample(example);
    }
}
